<?php
	class MY_Model extends CI_Model
	{
		protected $table;
		protected $tableLan;
		protected $table_id;
				
		function __construct()
		{
			// Call the Model constructor
			parent::__construct();			
		}
		
		function count_all($arr = array())
		{
			if(!empty($arr)){
				$this->db->where($arr);
				return $this->db->count_all_results($this->table);
			}
			else
				return $this->db->count_all($this->table);
		}
		
		function get_all($paging_value = 0, $start = 0, $order = "", $where = array())
		{
			
			if(!empty($order))
				$this->db->order_by($order);
			//$this->db->select('*');
			//$this->db->from($this->table);
			if(!empty($this->tableLan))
				$this->db->join($this->tableLan, "{$this->table}.{$this->table}_id = {$this->tableLan}.{$this->table}_id", 'left');
			/*
			if(!empty($paging_value)){
				if(!empty($start))
					$this->db->limit($paging_value, $start);
				else
					$this->db->limit($paging_value);
			}
			*/
			if(empty($paging_value))
				return $this->db->get_where($this->table, $where)->result();
			else
				return $this->db->get_where($this->table, $where, $paging_value, $start)->result();
		}
		
		function get_where($arr){
			if(!empty($this->tableLan))
				$this->db->join($this->tableLan, "{$this->table}.{$this->table}_id = {$this->tableLan}.{$this->table}_id", 'left');
			return $this->db->get_where($this->table, $arr);
		}
		
		function get_one($id){
			return $this->db->get_where($this->table, array($this->table_id => $id));
		}
		
		function check_url($arr = array()){
			$query = $this->db->get_where($this->tableLan, $arr);
			return $query;
		}
		
		function check_old_url($url, $arr = array()){
			$this->db->like($this->table . '_url', $url, 'after'); 
			$query = $this->db->get_where($this->tableLan, $arr);
			if($query->num_rows > 0)
				return $query->row()->{$this->table . "_url"};
			return "";
		}
		
		function get_max_url($url, $arr = array()){
			$this->db->select_max($this->table . '_url');
			$this->db->like($this->table . '_url', $url, 'after'); 
			$query = $this->db->get_where($this->tableLan, $arr);
			return $query;
		}
		
		function insert_entry($data, $auto_id = 1001)
		{
			$id = generate_id($this->table, $this->table_id, $auto_id);
			$data[$this->table_id] = $id;			
			$data["regDate"] = date("Y/m/d H:i:s", time());
			$data["upDate"] = $data["regDate"];	
			$this->db->insert($this->table, $data);			
			return $id;
		}
		
		function insert_entry_position($data, $auto_id = 1001)
		{
			$id = generate_id($this->table, $this->table_id, $auto_id);
			$data[$this->table_id] = $id;
			$position = $data["position"];
			if(empty($position)){
				$this->db->select_max('position');
				$query = $this->db->get($this->table);
				$position = $query->row()->position;
				$position++;
				$data["position"] = $position;
			}
			$data["regDate"] = date("Y/m/d H:i:s", time());
			$data["upDate"] = $data["regDate"];	
			$this->db->insert($this->table, $data);			
			return $id;
		}
		
		function update_entry($id, $data)
		{
			$data["upDate"] = date("Y/m/d H:i:s", time());
			$this->db->update($this->table, $data, array($this->table_id => $id));
		}
		
		function delete_entry($id)
		{
			if(!empty($this->tableLan))
				$this->db->delete($this->tableLan, array($this->table_id => $id));
			$this->db->delete($this->table, array($this->table_id => $id));
		}
	}
?>